<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="85%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="98px">
      <el-form-item label="请购单号" prop="nos">
        <el-input v-model="queryParams.nos" size="mini" placeholder="请输入订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="请购日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" size="mini" placeholder="请输入订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!--<el-form-item label="订单名称" prop="projectName">
        <el-input v-model="queryParams.projectName" size="mini" placeholder="请输入订单名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" size="mini" placeholder="请输入商品编码" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" size="mini" placeholder="请输入商品名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="goodModel">
        <el-input v-model="queryParams.goodModel" size="mini" placeholder="请输入商品型号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="columns" @selection-change="handleSelectionChange" :max-height="400">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" width="50" align="center"/>
      <el-table-column label="请购单号" align="center" prop="nos"/>
      <el-table-column label="订单日期" align="center" prop="ddDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求日期" align="center" prop="deliveryTime"/>
      <el-table-column label="订单单号" align="center" prop="projectNo"/>
      <el-table-column label="产品编码" align="center" prop="goodsCode"/>
      <el-table-column label="产品名称" align="center" prop="goodsName"/>
      <!--<el-table-column align="center" label="产品类别" prop="goodsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_type" :value="scope.row.goodsType"/>
        </template>
      </el-table-column>-->
      <el-table-column label="产品型号" align="center" prop="goodModel"/>
      <el-table-column label="产品单位" align="center" prop="unit"/>
      <el-table-column label="请购数量" align="center" prop="pleaseBuyCount"/>
      <el-table-column label="采购剩余" align="center" prop="buyRemain" fixed="right"/>
      <el-table-column label="采购数量" align="center" prop="buyCount" width="160" fixed="right">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.buyCount" size="mini" :max="scope.row.buyRemain"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary"  @click="submitForm" >确定选择</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <goods-salebuyLists ref="showForm" @ok="getList"></goods-salebuyLists>
  </el-dialog>
</template>

<script>
import {addAction, deleteAction, getAction, getById, updateAction} from '@/api/manage';
import {list} from "@/api/develop/goodsSaledo";
import GoodsSalebuyLists from "@/views/develop/lvBuy/module/GoodsSalebuyLists";

export default {
  name: "PleaseBuySelectList",
  dicts: ['goods_customer', 'company', 'yes_no', 'goods_supplier', 'goods_type'],
  components: {
    GoodsSalebuyLists
  },
  data() {
    return {
      proId: '',
      // 遮罩层
      title: "请购产品选择",
      loading: false,
      open: false,
      // 选中数组
      ids: [],
      buyId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库信息管理表格数据
      dataList: [],
      columns: [],
      dateRange: [],
      rules: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        model: null,
        costPrice: null,
        count: null,
        validityTerm: null,
        supplierId: null,
        supplierName: null,
        isdo: "0",
        isBuy: "1",
        projectNo: null,
        projectName: null,
        goodsCode: null,
        goodsName: null,
        goodModel: null,
        salesmanName: null,
        nos: null,
        params: {
          dataFlag: "1"
        }
      },
      url: {
        listBuyRemainNotZero: '/goodsSaledo/goodsSaledo/listBuyRemainNotZero',
        insertForBuy: '/goodsSalebuy/goodsSalebuy/insertForBuy',
      },
      tableMaxHeight: window.innerHeight - 900,
    };
  },
  created() {

  },
  methods: {
    reset() {
      this.form = {
        id: null,
        supplierId: null,
        supplierName: null,
        dingjin: null,
        bankName: null,
        bankNo: null,
        accountNo: null,
        remark: null,
        goodsType: "0",
      };
      this.resetForm("form");
    },

    show(buyId){
      this.open =true;
      this.buyId =buyId;
      this.getList();
    },

    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange);
      getAction(this.url.listBuyRemainNotZero, this.queryParams).then(response => {
        let res = response.rows;
        this.columns = res;
        for (let i = 0; i < this.columns.length; i++) {
          this.columns[i].buyCount = 0;
        }
        this.total = response.total;
        this.loading = false;
      });

    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.dataList = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 提交按钮 */
    submitForm() {
      let that = this;
      const ids = this.ids;

      if (ids == null || ids == '' || ids == 'undefined') {
        that.$modal.msgError("请选择一条记录");
        return;
      }

      for (let i = 0; i < that.dataList.length; i++) {
        if (that.dataList[i].buyCount === 0) {
          that.$modal.msgError("采购数量不能为0");
          return;
        }
      }
        const goodsSaledo = Object.assign({});
        goodsSaledo.mainId = this.buyId;
        goodsSaledo.columns = that.dataList;
        addAction(that.url.insertForBuy, goodsSaledo).then(response => {
          that.$modal.msgSuccess("添加成功");
          that.open = false;
          that.$emit('ok');
        });
    },

  }
};
</script>
